package com.cuotigou.controller;

import com.alibaba.fastjson.JSON;
import com.cuotigou.mapper.SConfigMapper;
import com.cuotigou.mapper.SOfferRewardAnswerMapper;
import com.cuotigou.mapper.SOfferRewardMapper;
import com.cuotigou.mapper.SUserMapper;
import com.cuotigou.pojo.SOfferReward;
import com.cuotigou.pojo.SUser;
import com.cuotigou.service.OfferRewardService;
import com.cuotigou.util.*;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/7.
 */
@Controller
@RequestMapping("/offerReward")
public class OfferRewardController {
    @Autowired
    private OfferRewardService offerRewardService;
    @Autowired
    private SOfferRewardMapper sOfferRewardMapper;
    @Autowired
    private SUserMapper sUserMapper;
    @Autowired
    private SConfigMapper sConfigMapper;
    @Autowired
    private SOfferRewardAnswerMapper sOfferRewardAnswerMapper;

    /**
     * 热门悬赏
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult index(XMLBean xmlBean){
//        if (StringUtil.isEmpty(xmlBean.getToken())){
//            return ObjectResult.build("0","未登录");
//        }
     return offerRewardService.index(xmlBean);
    }

    /**
     * 悬赏积分
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/integral", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult integral(XMLBean xmlBean){
        if (StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        String userId = CommonUtils.decToken(xmlBean.getToken());
        SUser sUser = sUserMapper.selectByPrimaryKey(Long.parseLong(userId));

        Map<String,Object> map = new HashMap<>();
        map.put("integral",sUser.getIntegral());
        Integer [] array = {0,10,20,50,100,200};
        map.put("array",array);

        return ObjectResult.build("1","成功",map);
    }



    /**
     * 发布悬赏
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/release", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult release(XMLBean xmlBean,HttpServletRequest request,@RequestParam("files")MultipartFile[] files){
        if (StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        if (xmlBean.getIntegral()==null || StringUtil.isEmpty(xmlBean.getContent())){
            ObjectResult.build("0", "参数丢失请重试");
        }
        return offerRewardService.offerRewardRelease(xmlBean,files);
    }



    /**
     * 删除悬赏
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult delete(XMLBean xmlBean){
        if (StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        if(StringUtil.isEmpty(xmlBean.getId())){
            return ObjectResult.build("0","参数丢失请重试");

        }
        return offerRewardService.offerRewardDelete(xmlBean);
    }

    /**
     * 修改悬赏
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult update(XMLBean xmlBean){
        if (StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        if(StringUtil.isEmpty(xmlBean.getId()) || xmlBean.getIntegral() == null ){
            return ObjectResult.build("0","参数丢失请重试");

        }
        return offerRewardService.offerRewardUpdate(xmlBean);
    }
    /**
     * 悬赏详情
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/details", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult details(XMLBean xmlBean, HttpServletResponse response){
//        if (StringUtil.isEmpty(xmlBean.getToken())){
//            return ObjectResult.build("0","未登录");
//        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        return offerRewardService.offerRewardDetails(xmlBean);
    }

    /**
     * 我的悬赏
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/my", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult my(XMLBean xmlBean){
        if (StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }
        return offerRewardService.myOfferReward(xmlBean);
    }

    /**
     * 添加悬赏回答
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/answer", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult answer(XMLBean xmlBean,@RequestParam("files")MultipartFile[] files) {
        if (StringUtil.isEmpty(xmlBean.getToken())) {
            return ObjectResult.build("0", "未登录");
        }
        return offerRewardService.offerRewardanswer(xmlBean,files);
    }

    /**
     * 答题排行榜
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/rank", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult rank(XMLBean xmlBean) {
        String rank = sConfigMapper.select("rank");
        return ObjectResult.build("1","成功",sUserMapper.selectAnswerRank(Integer.parseInt(rank)));
    }


    /**
     * 悬赏回答采纳
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/adopt", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult adopt(XMLBean xmlBean) {
        if (StringUtil.isEmpty(xmlBean.getToken())) {
            return ObjectResult.build("0", "未登录");
        }
        return offerRewardService.offerRewardAdopt(xmlBean);
    }

    /**
     * 悬赏加入错题本
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/addWrongTopic", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult addWrongTopic(XMLBean xmlBean) {
        if (StringUtil.isEmpty(xmlBean.getToken())) {
            return ObjectResult.build("0", "未登录");
        }
        return offerRewardService.addWrongTopic(xmlBean);
    }

    /**
     * 悬赏举报点赞及取消
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/answerFabulous", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult answerFabulous(XMLBean xmlBean) {
        if (StringUtil.isEmpty(xmlBean.getToken())) {
            return ObjectResult.build("0", "未登录");
        }
        return offerRewardService.offerRewardanswerFabulous(xmlBean);
    }

    /**
     * 悬赏回答已读
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/see", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ObjectResult see(XMLBean xmlBean) {
        if (StringUtil.isEmpty(xmlBean.getToken()) || StringUtil.isEmpty(xmlBean.getId())) {
            return ObjectResult.build("0","参数丢失请重试");
        }

        sOfferRewardAnswerMapper.update(Integer.parseInt(xmlBean.getId()));

        return ObjectResult.build("1","成功");
    }
}
